package com.sbtr.common.file.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileUtil {

    public static String getEncode(MultipartFile multipartFile) {

        String str = "";
        String encode = "UTF-8";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(multipartFile.getInputStream(), encode));
            str = br.readLine() + br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (!isMessyCode(str)) {
            return encode;
        }

        encode = "GB2312";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(multipartFile.getInputStream(), encode));
            str = br.readLine() + br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (!isMessyCode(str)) {
            return encode;
        }

        encode = "GBK";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(multipartFile.getInputStream(), encode));
            str = br.readLine() + br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (!isMessyCode(str)) {
            return encode;
        }

        encode = "ISO-8859-1";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(multipartFile.getInputStream(), encode));
            str = br.readLine() + br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (!isMessyCode(str)) {
            return encode;
        }
        return "";
    }

    private static boolean isMessyCode(String strName) {
        try {
//			if(strName.length() >= 30){
//				strName = strName.substring(0, 30);
//			}

            Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
            Matcher m = p.matcher(strName);
            String after = m.replaceAll("");
            String temp = after.replaceAll("\\p{P}", "");
            temp = temp.replaceAll("[\\u00A0]+", "");
            temp = temp.replaceAll("\uFEFF", "");
            String regEx = "[\n`~!@#$%^&*￣()+=|{}':;',\\[\\].<>/?~！@#￥%……&*()——+|{}【】‘；：”“’。， 、？]";
            temp = temp.replaceAll(regEx, "");
            char[] ch = temp.trim().toCharArray();

//			int length = (ch != null) ? ch.length : 0;
//			if(length >= 30){
//				length = 30;
//			}
            int length = ch.length;
            for (int i = 0; i < length; i++) {
                char c = ch[i];
                if (!Character.isLetterOrDigit(c)) {
                    String str = "" + ch[i];
                    if (!str.matches("[\u4e00-\u9fa5]+")) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
